<template>
  <div class="list-container">
    <div class="search-wrap clearfix">
      <div class="search-wrap-conditions fl marginL10">
        <el-select
          v-model="listQuery.condition.status"
          clearable
          placeholder="请选择状态"
          @change="resetPage"
        >
          <el-option
            v-for="item in optionsInitiator"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </div>
      <div class="search-wrap-conditions fl marginL10">
        <el-input
          v-model="listQuery.condition.keyword"
          class="input-width"
          placeholder="标题"
          @keyup.enter.native="resetPage"
        />
      </div>
      <div class="search-wrap-conditions fl marginL10">
        <div class="block">
          <el-date-picker
            v-model="timer"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="yyyy-MM-dd HH:mm:ss"
            @change="resetPage"
          />
        </div>
      </div>
      <div class="search-wrap-btn fr">
        <el-button :disabled="loading" @click="resetSearch">重置</el-button>
        <el-button :disabled="loading" type="primary" @click="resetPage">搜索</el-button>
        <!-- <el-button :disabled="loading" type="success" @click="add">添加</el-button> -->
      </div>
    </div>
    <div class="main-wrapper">
      <custom-table :data="listData" :columns="columns">
        <template slot-scope="{ row }">
          <keep-alive>
            <el-button type="text" @click="getDetail(row.activityId)">查看详情</el-button>
          </keep-alive>
        </template>
      </custom-table>
      <pagination :total="total" :page="listQuery.pager.page" @pagination="pagination" />
    </div>
    <el-dialog title="完善分类信息" :visible.sync="dialogVisible" width="30%">
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="名称">
          <el-input />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button>取 消</el-button>
        <el-button type="primary">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import CustomTable from '@/components/CustomTable/index';
import Pagination from '@/components/Pagination/index';
import { optionsStatus, optionsInitiator } from './mock';
import { getUsersList } from '@/api/activity';

export default {
  name: 'List',
  components: {
    CustomTable,
    Pagination
  },
  data () {
    return {
      dialogVisible: false,
      form: {
        atvDescribe: '', // 活动描述
        categoryList: null, // 分类
        coverImageId: '', // 封面图
        endTime: '', // 活动结束时间
        enrollFee: '', // 报名费
        quota: '', // 限制参与认输
        startTime: '', // 活动开始时间
        tagList: null, // 标签
        title: '', // 标题
        webCastType: '' // 在线课程类型
      },
      optionsStatus: optionsStatus,
      optionsInitiator: optionsInitiator,
      loading: false,
      timer: [],
      listQuery: {
        condition: {
          startTime: null,
          endTime: null,
          keyword: null,
          status: null,
          userId: null
        },
        pager: {
          page: 1,
          pageSize: 10
        }
      },
      listData: [],
      total: 0,
      columns: [
        { label: '序号', value: 'index', align: 'center', width: '80px' },
        { label: '标题', value: 'title', align: 'center' },
        { label: '状态', value: 'statusTxt', align: 'center', width: '100px' },
        { label: '发起者', value: 'userName', align: 'center', width: '160px' },
        { label: '时间', value: 'createTime', align: 'center', width: '140px' },
        { label: '操作', value: 'todo', align: 'center', width: '120px' }
      ]
    };
  },
  created () {
    this.init();
  },
  methods: {
    add () {
      this.$router.push({
        path: '/activity/liveEditOrAdd'
      });
    },
    resetPage () {
      this.listQuery.pager.page = 1;
      this.listQuery.pager.pageSize = 10;
      this.init();
    },
    init () {
      this.listQuery.condition.startTime = this.timer && this.timer.length > 0 ? this.timer[0] : null;
      this.listQuery.condition.endTime = this.timer && this.timer.length > 0 ? this.timer[1] : null;
      this.loading = true;
      this.listQuery.condition.keyWord = this.listQuery.condition.keyWord ? this.listQuery.condition.keyWord : null;
      getUsersList(this.listQuery)
        .then(res => {
          if (this.checkArr(res.data.records)) {
            res.data.records.map(item => {
              console.log('item.status:', item.status);
              switch (item.status) {
                case 'NOT_STARTED':
                  item.statusTxt = '未开始';
                  break;
                case 'ONGOING':
                  item.statusTxt = '进行中';
                  break;
                case 'END':
                  item.statusTxt = '已结束';
                  break;
                case 'VIOLATE':
                  item.statusTxt = '违规';
                  break;
                case 'UNAUDITED':
                  item.statusTxt = '待审核';
                  break;
              }
            });
          }
          this.listData = res.data.records;
          this.total = res.data.total;
          this.loading = false;
        })
        .catch(error => {
          this.loading = false;
        });
    },
    resetSearch () {
      this.timer = [];
      this.listQuery = {
        condition: {
          startTime: null,
          endTime: null,
          keyword: null,
          status: null,
          userId: null
        },
        pager: {
          page: 1,
          pageSize: 10
        }
      };
      this.init();
    },
    pagination ({ page, limit }) {
      this.listQuery.pager.page = page;
      this.listQuery.pager.pageSize = limit;
      this.init();
    },
    getDetail (id) {
      // this.$router.push({
      //   path: '/activity/activityDetail',
      //   query: { param: id }
      // });
      const routeData = this.$router.resolve({ path: '/activity/activityDetail', query: { param: id } });
      window.open(routeData.href, '_blank');
    }
  }
};
</script>

<style lang="scss" scoped></style>
